package com.cloudrui.service.wechat;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.cloudrui.service.wechat.neteasy.NetEasyMatchService;
import com.cloudrui.service.wechat.neteasy.NetLiveService;
import com.cloudrui.service.wechat.neteasy.bean.LiveBean;

import cn.zhouyafeng.itchat4j.api.MessageTools;
import cn.zhouyafeng.itchat4j.api.WechatTools;
import cn.zhouyafeng.itchat4j.beans.BaseMsg;
import cn.zhouyafeng.itchat4j.face.IMsgHandlerFace;

public class WeChatMsgHandler implements IMsgHandlerFace {
	public static List<JSONObject> groupList = null;
	public static List<JSONObject> contactList = null;
	private static String id = null;
	
	private static String mySelfId = null;
	
	private static boolean needLive = false;

	public String textMsgHandle(BaseMsg msg) {
		if (groupList == null) {
			groupList = WechatTools.getGroupList();
		}
		if(mySelfId == null) {
			JSONObject selfObj = WechatToolsEx.getUserSelf();
			mySelfId = selfObj.getString("UserName");
		}
		
		
		String text = msg.getText();
//		String toUserName = msg.getToUserName();
//		String fromUserName = msg.getFromUserName();
//		for(JSONObject obj:groupList) {
//			String nickName = obj.getString("NickName");
//			if(nickName.equals("一号秘书处")) {
//				System.out.println("toUserName=="+toUserName);
//				System.out.println("fromUserName=="+fromUserName);
//				System.out.println(obj.toJSONString());
//			}
//			
//		}
		String fromId = msg.getFromUserName();
		String team = null;
		if(fromId.equalsIgnoreCase(mySelfId)) {
			if(text.startsWith(("开始直播@"))) {
				String[] teams = text.split("@");
				if(teams.length==2) {
					team = teams[1];
				}else {
					return null;
				}
				needLive = true;
				if (id == null) {
					for (JSONObject jsonObj : groupList) {
						String nickName = jsonObj.getString("NickName");
						System.out.println("nickName="+nickName);
						if (nickName.equalsIgnoreCase("神六")) {
							id = jsonObj.getString("UserName");
						}
					}
				}
				LiveBean liveBean = NetEasyMatchService.getLiveBean(team);
				if(liveBean == null) {
					MessageTools.sendMsgById("今天冒得"+team+"的比赛", id);
					return null;
				}
				
				NetLiveService t = new NetLiveService(liveBean.getUrl(), id,liveBean.getMcode());
				Thread thread = new Thread(t);
				thread.start();
			}else if(text.equals("停止直播")) {
				needLive = false;
			}
		}
		return null;
	}

	@Override
	public String picMsgHandle(BaseMsg arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String voiceMsgHandle(BaseMsg arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String viedoMsgHandle(BaseMsg arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String nameCardMsgHandle(BaseMsg arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void sysMsgHandle(BaseMsg arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public String verifyAddFriendMsgHandle(BaseMsg arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String mediaMsgHandle(BaseMsg arg0) {
		// TODO Auto-generated method stub
		return null;
	}
	
	public static boolean needLive() {
		return needLive;
	}
	
}
